<?php

namespace app\api\controller\resource;
use app\api\service\resource\LibWordEnTypeService;
use app\BaseController;
use app\api\service\resource\LibWordEnMeaningService;
use app\util\CommonUtil;
use app\api\service\develop\DatagridColumnService;
use app\api\service\log\AdminOperateLogService;
use app\util\AdminUserUtil;
use app\util\OptionUtil;
use app\util\ResponseUtil;
use think\App;
use think\facade\Db;

class LibWordEnMeaning extends BaseController
{

    private $pageCode = 'page_toolbar';
    private $arrFieldTitle = array("status" => "状态");

    public function __construct(App $app)
    {
        parent::__construct($app);
        $serviceDatagridColumn = new DatagridColumnService();
        $list = $serviceDatagridColumn->getListNoPage(array('page_code' => $this->pageCode, 'status' => 1));
        $this->arrFieldTitle = array_column($list, 'column_title', 'column_name');
    }

    public function getLibWordEnMeaningInfo()
    {
        $id = $this->requestData['id'];
        $service = new LibWordEnMeaningService();
        $finalData = $service->findRowById($id);
        return ResponseUtil::success($finalData);
    }

    public function getLibWordEnMeaningList()
    {
        $pageIndex = $this->requestData['page'];
        $pageSize = $this->requestData['rows'];
        $service = new LibWordEnMeaningService();
        $list = $service->getListWithPage($this->requestData, $pageIndex, $pageSize);
        $total = $service->getTotalNum($this->requestData);
        $finalData = CommonUtil::getPaginationData($list, $total);
        return ResponseUtil::success($finalData);
    }

    public function getLibWordEnMeaningOption()
    {
        $service = new LibWordEnMeaningService();
        $list = $service->getListNoPage($this->requestData);
        return ResponseUtil::success($list);
    }

    public function addLibWordEnMeaning()
    {
        try{
            if(empty($this->requestData['word_id'])){
                return ResponseUtil::failure("请指定单词");
            }

            if(empty($this->requestData['word_en_type'])){
                return ResponseUtil::failure("请选择词性");
            }

            $model = Db::table("yj_lib_word_en_type");
            $model->startTrans();
            $wordTypeValue = $this->requestData['word_en_type'];
            $wordTypeOption = OptionUtil::getWordTypeOption();
            $wordTypeOptionByValue = array_column($wordTypeOption, 'text', 'value');
            $wordTypeData = array(
                'part_of_speech' => $wordTypeValue,
                'speech_text' => $wordTypeOptionByValue[$wordTypeValue],
                'word_id' => $this->requestData['word_id']
            );
            $serviceWordType = new LibWordEnTypeService();
            $newWordTypeId = $serviceWordType->createRow($wordTypeData);

            $serviceWordMeaning = new LibWordEnMeaningService();
            $addFlag = true;
            foreach($this->requestData['meaning_data'] as $meaningDataItem){
                if(empty($meaningDataItem['meaning_cn'])){
                    continue;
                }
                $meaningData = array_merge($meaningDataItem, array('word_type_id' => $newWordTypeId));
                $res = $serviceWordMeaning->createRow($meaningData);
                if(!$res){
                    $model->rollback();
                    $addFlag = false;
                }
            }
            if($addFlag){
                $model->commit();
                $message = '新增单词释义';
                $logData = [
                    'subject_id' => $res,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('LibWordEnMeaning', AdminOperateLogService::ARR_ACTION_TYPE['add'], $logData);
                return ResponseUtil::success($res);
            }else{
                return ResponseUtil::failure("新增单词释义失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("新增单词释义失败");
        }
    }

    public function editLibWordEnMeaning()
    {
        try{
            if(empty($this->requestData['id'])){
                return ResponseUtil::failure("请指定要编辑的单词释义");
            }

            $id = $this->requestData['id'];
            $service = new LibWordEnMeaningService();
            $oldInfo = $service->findRowById($id);
            if(empty($oldInfo)){
                return ResponseUtil::failure("数据不存在");
            }

            $res = $service->updateRowById($id, $this->requestData);
            if($res){
                $changeInfo = CommonUtil::getChangeData($oldInfo, $this->requestData, $this->arrFieldTitle);
                $message = '编辑单词释义,'.$changeInfo['update_info'];
                $logData = [
                    'subject_id' => $id,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('LibWordEnMeaning', AdminOperateLogService::ARR_ACTION_TYPE['edit'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("编辑单词释义失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("编辑单词释义失败");
        }
    }

    public function deleteLibWordEnMeaning()
    {
        try{
            $id = $this->requestData['id'];
            $service = new LibWordEnMeaningService();
            $info = $service->findRowById($id);
            if(!$info){
                return ResponseUtil::failure("数据不存在");
            }

            $res = $service->removeRowById($id);
            if($res){
                $deleteInfo = CommonUtil::getDataDeleteMessage($info, $this->arrFieldTitle);
                $message = '删除单词释义';
                if($deleteInfo){
                    $message .= ','.$deleteInfo;
                }
                $logData = [
                    'subject_id' => $id,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('LibWordEnMeaning', AdminOperateLogService::ARR_ACTION_TYPE['delete'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("删除单词释义失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("删除单词释义失败");
        }
    }

    public function deleteMultiLibWordEnMeaning(){
        try{
            $id = $this->requestData['id'];
            if(!is_array($id)){
                return ResponseUtil::failure("参数格式有误");
            }
            $service = new LibWordEnMeaningService();
            $deleteData = [];
            foreach($id as $value){
                $info = $service->findRowById($value);
                if(!$info){
                    return ResponseUtil::failure("单词释义不存在");
                }
                $deleteData[] = $info;
            }
            $res = $service->removeListByManyId($id);
            if($res){
                $message = '删除单词释义';
                $logData = [
                    'subject_id' => json_encode($id),
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('LibWordEnMeaning', AdminOperateLogService::ARR_ACTION_TYPE['delete'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("删除单词释义失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("删除单词释义失败");
        }
    }

    public function simpleUpdateLibWordEnMeaning()
    {
        try{
            if(empty($this->requestData['id'])){
                return ResponseUtil::failure("请指定要编辑的单词释义");
            }

            $id = $this->requestData['id'];
            $service = new LibWordEnMeaningService();
            $oldInfo = $service->findRowById($id);
            if(empty($oldInfo)){
                return ResponseUtil::failure("数据不存在");
            }
            $res = $service->updateRowById($id, $this->requestData);
            if($res){
                $changeInfo = CommonUtil::getChangeData($oldInfo, $this->requestData, $this->arrFieldTitle);
                $message = '编辑单词释义,'.$changeInfo['update_info'];
                $logData = [
                    'subject' => $id,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('LibWordEnMeaning', AdminOperateLogService::ARR_ACTION_TYPE['edit'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("编辑单词释义失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("编辑单词释义失败");
        }
    }

}



